﻿using Direct.IDAL.User.Money;
using DirectSell.Common.CodeHelp;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.Common.Session;
using DirectSell.Common.Url;
using DirectSell.Model.Money;
using DirectSell.Model.User;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.WebLayer.Areas.User.Controllers
{
    /// <summary>
    /// 内部转账
    /// </summary>
    public class UserGiroController : Controller
    {
        #region 注入
        [Dependency("UserGiro")]
        protected IBLL.IOperation _service { get; set; }

        [Dependency]
        protected IUserGiroRepository _repository { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck =true)]
        public async Task<ActionResult> Index(int id = 1, int pageIndex = 1)
        {
            var sessionmodel = SessionInfo.GetSession();

            ViewBag.UserCode = sessionmodel.UserCode;

            var hushModel = await _repository.GetHushRecord(sessionmodel.UserCode, pageIndex, CommonConfig.PageSize);

            var reciveModel = await _repository.GetReciveRecord(sessionmodel.UserCode,id,CommonConfig.PageSize);

           PageUserGiro pagelist = new PageUserGiro()
           {
            UserGiro_1 = hushModel,
            UserGiro_2 = reciveModel,
           };
            return View(pagelist);
        }
        #endregion

        #region 转账操作
        /// <summary>
        /// 转账操作
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public UserRespCode Giro(UserGiroReq req)
        {
            req.UserName = SessionInfo.GetSession().UserName;
            //判断验证码是否正确
            if (SessionHelper.GetSession("code3") == null || req.CheckCode != SessionHelper.GetSession("code3").ToString())
            {
                //验证码错误
                return UserRespCode.验证码错误;
            }
           return  _service.DoOperation(req);
        }
        #endregion

        #region 获取图片验证码
        /// <summary>
        /// 获取图片验证码 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImageValiDateCode()
        {
            //获得验证码
            string code = ValidateCode.GetRandNumber(4);
            byte[] bytes = ValidateCode.CreateCheckCode(code);
            //验证码写入Session
            SessionHelper.SetSession("code3", code);
            return File(bytes, @"image/jpeg");
        }
        #endregion

    }
}